<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-07-07
 * Time: 10:48
 * 财务记录
 */

namespace app\api\controller\v1;


use app\api\model\GoodsOrder;
use app\api\model\LinUser;
use app\api\model\StoreOrder;
use app\api\model\UserMoney;

class Finance
{
    /**
     * 商城订单
     * @validate('PagingParameter')
     * @auth('财务记录-商品订单记录','财务记录')
     */
    public function getGoodsList($page,$limit,$user_no,$name,$mobile,$address,$status,$start_date,$end_date)
    {
        $where = [];
        if($name) {
            $where[] = ['name','like','%'.$name.'%'];
        }
        if($mobile) {
            $where[] = ['mobile','like','%'.$mobile.'%'];
        }
        if($address) {
            $where[] = ['address','like','%'.$address.'%'];
        }
        if($status) {
            $where[] = ['goods_order.status', 'eq' ,$status];
        } else {
            $where[] = ['goods_order.status', 'gt' ,1];
        }
        if($start_date) {
            $where[] = ['pay_time', 'gt', strtotime($start_date)];
        }
        if($end_date) {
            $where[] = ['pay_time', 'lt', strtotime($end_date)];
        }
        $query = GoodsOrder::hasWhere('user',function ($query1) use($user_no){
            if($user_no) {
                $query1->where('user_no','like','%'.$user_no.'%');
            }
        })->with('content.img,user')->where($where)->order('pay_time desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['status'] == 2) {
                $item['status_name'] = '待发货';
            } elseif($item['status'] == 3){
                $item['status_name'] = '待收货';
            } elseif($item['status'] == 4){
                $item['status_name'] = '已完成';
            } elseif($item['status'] == 5){
                $item['status_name'] = '已申请退款';
            } elseif($item['status'] == 6){
                $item['status_name'] = '退款完成';
            }
            if($item['payment'] == 1) {
                $item['payment_name'] = '微信支付';
            } elseif($item['payment'] == 2) {
                $item['payment_name'] = '余额支付';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取洗衣订单列表
     * @validate('PagingParameter')
     * @auth('财务记录-洗衣订单记录','财务记录')
     */
    public function getStoreList($page,$limit,$user_no,$name,$mobile,$address,$status,$start_date,$end_date,$lin_user_id)
    {
        $where = [];
        $linUser = LinUser::where('id', $lin_user_id)->find();
        if ($linUser['admin'] == 1) {
            $where[] = ['lin_user_id', 'eq', $lin_user_id];
        }
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        if ($mobile) {
            $where[] = ['mobile', 'like', '%' . $mobile . '%'];
        }
        if ($address) {
            $where[] = ['address', 'like', '%' . $address . '%'];
        }
        if ($status) {
            $where[] = ['status', 'eq', $status];
        }
        if ($start_date) {
            $where[] = ['store_order.create_time', 'gt', strtotime($start_date)];
        }
        if ($end_date) {
            $where[] = ['store_order.create_time', 'lt', strtotime($end_date)];
        }
        $query = StoreOrder::hasWhere('user',function ($query1) use($user_no){
            if($user_no) {
                $query1->where('user_no','like','%'.$user_no.'%');
            }
        })->with('content,user')->where($where)->order('store_order.create_time desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item) {
            if ($item['status'] == 1) {
                $item['status_name'] = '待取送员抢单';
            } elseif ($item['status'] == 2) {
                $item['status_name'] = '待上门取衣';
            } elseif ($item['status'] == 3) {
                $item['status_name'] = '待支付';
            } elseif ($item['status'] == 4) {
                $item['status_name'] = '待用户预约配送';
            } elseif ($item['status'] == 5) {
                $item['status_name'] = '正在配送';
            } elseif ($item['status'] == 6) {
                $item['status_name'] = '已完成';
            }
            if ($item['payment'] == 1) {
                $item['payment_name'] = '微信支付';
            } elseif ($item['payment'] == 2) {
                $item['payment_name'] = '余额支付';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取洗衣订单列表
     * @validate('PagingParameter')
     * @auth('财务记录-用户余额记录','财务记录')
     */
    public function getUserMoneyList($page,$limit,$grade_name,$user_no,$nickname,$type,$start_date,$end_date)
    {
        $where = [];
        if ($type) {
            $where[] = ['type', 'eq', $type];
        }
        if ($start_date) {
            $where[] = ['user_money.create_time', 'gt', strtotime($start_date)];
        }
        if ($end_date) {
            $where[] = ['user_money.create_time', 'lt', strtotime($end_date)];
        }
        $query = UserMoney::hasWhere('user',function($query1) use($user_no,$nickname,$grade_name){
            if($grade_name) {
                if($user_no) {
                    if($nickname) {
                        $query1->where('nickname','like','%'.$nickname.'%')
                            ->where('user_no','eq',$user_no)
                            ->where('grade_name','like','%'.$grade_name.'%');
                    } else {
                        $query1->where('user_no','eq',$user_no)->where('grade_name','like','%'.$grade_name.'%');
                    }
                } else {
                    if($nickname) {
                        $query1->where('nickname','like','%'.$nickname.'%')->where('grade_name','like','%'.$grade_name.'%');
                    } else {
                        $query1->where('grade_name','like','%'.$grade_name.'%');
                    }
                }
            } else {
                if($user_no) {
                    if($nickname) {
                        $query1->where('nickname','like','%'.$nickname.'%')->where('user_no','eq',$user_no);
                    } else {
                        $query1->where('user_no','eq',$user_no);
                    }
                } else {
                    if($nickname) {
                        $query1->where('nickname','like','%'.$nickname.'%');
                    }
                }
            }
        })->with('user')->where($where)->order('user_money.create_time desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item) {
            if($item['type'] == 1) {
                $item['type_name'] = '余额充值得到';
            } elseif($item['type'] == 2) {
                $item['type_name'] = '支付订单失去';
            } elseif($item['type'] == 3) {
                $item['type_name'] = '直接使用失去';
            } elseif($item['type'] == 4) {
                $item['type_name'] = '后台操作得到';
            } elseif($item['type'] == 5) {
                $item['type_name'] = '后台操作失去';
            } elseif($item['type'] == 6) {
                $item['type_name'] = '公众号余额得到';
            } elseif($item['type'] == 7) {
                $item['type_name'] = '申请退款得到';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取洗衣订单列表
     * @auth('财务记录-用户余额记录','财务记录')
     */
    public function getUserMoneyExcelList($grade_name,$user_no,$nickname,$type,$start_date,$end_date)
    {
        $where = [];
        if ($type) {
            $where[] = ['type', 'eq', $type];
        }
        if ($start_date) {
            $where[] = ['user_money.create_time', 'gt', strtotime($start_date)];
        }
        if ($end_date) {
            $where[] = ['user_money.create_time', 'lt', strtotime($end_date)];
        }
        return UserMoney::hasWhere('user',function($query1) use($user_no,$nickname,$grade_name){
            if($grade_name) {
                if($user_no) {
                    if($nickname) {
                        $query1->where('nickname','like','%'.$nickname.'%')
                            ->where('user_no','eq',$user_no)
                            ->where('grade_name','like','%'.$grade_name.'%');
                    } else {
                        $query1->where('user_no','eq',$user_no)->where('grade_name','like','%'.$grade_name.'%');
                    }
                } else {
                    if($nickname) {
                        $query1->where('nickname','like','%'.$nickname.'%')->where('grade_name','like','%'.$grade_name.'%');
                    } else {
                        $query1->where('grade_name','like','%'.$grade_name.'%');
                    }
                }
            } else {
                if($user_no) {
                    if($nickname) {
                        $query1->where('nickname','like','%'.$nickname.'%')->where('user_no','eq',$user_no);
                    } else {
                        $query1->where('user_no','eq',$user_no);
                    }
                } else {
                    if($nickname) {
                        $query1->where('nickname','like','%'.$nickname.'%');
                    }
                }
            }
        })->with('user')->where($where)->order('user_money.create_time desc')->select()->each(function ($item) {
            if($item['type'] == 1) {
                $item['type_name'] = '余额充值得到';
            } elseif($item['type'] == 2) {
                $item['type_name'] = '支付订单失去';
            } elseif($item['type'] == 3) {
                $item['type_name'] = '直接使用失去';
            } elseif($item['type'] == 4) {
                $item['type_name'] = '后台操作得到';
            } elseif($item['type'] == 5) {
                $item['type_name'] = '后台操作失去';
            } elseif($item['type'] == 6) {
                $item['type_name'] = '公众号余额得到';
            } elseif($item['type'] == 7) {
                $item['type_name'] = '申请退款得到';
            }
        })->toArray();
    }
}